package com.example.tijianserver.controller;

import com.example.tijianserver.mapper.UsersMapper;
import com.example.tijianserver.pojo.Users;
import com.example.tijianserver.response.ApiRestResponse;
import com.example.tijianserver.response.TijianExceptionEnum;
import com.example.tijianserver.service.UsersService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.example.tijianserver.utils.MD5Util.MD5Encrypt;

//@CrossOrigin
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    UsersService usersService;

    @RequestMapping("/login")  //所以url请求：  http://localhost:8080/users/login   记住还要参数json字符串
    public ApiRestResponse<Users> login(@RequestBody Users users){
        // 先加密密码
        String encryptedPassword = MD5Encrypt(users.getPassword());
        users.setPassword(encryptedPassword);

        Users user=usersService.login(users);
        return ApiRestResponse.success(user);
    }

    @PostMapping("/register")
    public ApiRestResponse<Integer> register(@RequestBody Users users) {
        // 插入新用户前先加密密码
        String encryptedPassword = MD5Encrypt(users.getPassword());
        users.setPassword(encryptedPassword);

        int result = usersService.register(users);
            return ApiRestResponse.success(result);
    }
    @PostMapping("/forgot")
    public ApiRestResponse<Users> forgot(@RequestBody Users users){
        Users user=usersService.forgot(users);
        return ApiRestResponse.success(user);
    }

    @PostMapping("/updatePassword")
    public ApiRestResponse<Boolean> updatePassword(@RequestBody Users users) {
        // 插入新用户前先加密密码
        String encryptedPassword = MD5Encrypt(users.getPassword());
        users.setPassword(encryptedPassword);

        boolean result = usersService.updatePassword(users);
            return ApiRestResponse.success(result);
    }

}
